﻿@inject IStringLocalizer<TablesHeaderNormal> Localizer
@inject IStringLocalizer<Foo> LocalizerFoo

<Table TItem="Foo" OnQueryAsync="@OnQueryAsync" IsBordered="true" IsStriped="true" ShowMultiFilterHeader="false">
    <MultiHeaderTemplate>
        <tr>
            <th rowspan="2" class="table-header-group-th1"><div class="table-cell">@Localizer["TablesHeaderNormal_Time"]</div></th>
            <th colspan="2" class="table-header-group-th2"><div class="table-cell">@Localizer["TablesHeaderNormal_Info"]</div></th>
        </tr>
        <tr>
            <th><div class="table-cell">@Localizer["TablesHeaderNormal_Name"]</div></th>
            <th><div class="table-cell">@Localizer["TablesHeaderNormal_Address"]</div></th>
        </tr>
    </MultiHeaderTemplate>
    <TableColumns>
        <TableColumn @bind-Field="@context.DateTime" Width="180" Filterable="true" Sortable="true" />
        <TableColumn @bind-Field="@context.Name" Width="100" Filterable="true" Sortable="true" />
        <TableColumn @bind-Field="@context.Address" Filterable="true" Sortable="true" />
    </TableColumns>
</Table>

@code {
    /// <summary>
    /// Foo 类为Demo测试用，如有需要请自行下载源码查阅
    /// Foo class is used for Demo test, please download the source code if necessary
    /// https://gitee.com/LongbowEnterprise/BootstrapBlazor/blob/main/src/BootstrapBlazor.Shared/Data/Foo.cs
    /// </summary>
    [NotNull]
    private List<Foo>? Items { get; set; }

    /// <summary>
    /// OnInitialized
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();
        Items = Foo.GenerateFoo(LocalizerFoo, 4);
    }

    private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)
    {
        IEnumerable<Foo> items = Items;

        var isFiltered = false;
        if (options.Filters.Any())
        {
            items = items.Where(options.Filters.GetFilterFunc<Foo>());
            isFiltered = true;
        }

        var isSorted = false;
        if (!string.IsNullOrEmpty(options.SortName))
        {
            var invoker = Foo.GetNameSortFunc();
            items = invoker(items, options.SortName, options.SortOrder);
            isSorted = true;
        }

        var total = items.Count();
        items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();
        return Task.FromResult(new QueryData<Foo>()
        {
            Items = items,
            TotalCount = total,
            IsSorted = isSorted,
            IsFiltered = isFiltered,
            IsSearch = true
        });
    }
}
